﻿using System;
using System.Collections;
using System.IO;
using System.Text;
using Illusion.CustomAttributes;
using UnityEngine;

// Token: 0x0200036A RID: 874
public class GameScreenShot : MonoBehaviour
{
	// Token: 0x060015D7 RID: 5591 RVA: 0x00093200 File Offset: 0x00091400
	public string CreateCaptureFileName()
	{
		StringBuilder stringBuilder = new StringBuilder(256);
		stringBuilder.Append(UserData.Create("cap"));
		DateTime now = DateTime.Now;
		stringBuilder.Append(now.Year.ToString("0000"));
		stringBuilder.Append(now.Month.ToString("00"));
		stringBuilder.Append(now.Day.ToString("00"));
		stringBuilder.Append(now.Hour.ToString("00"));
		stringBuilder.Append(now.Minute.ToString("00"));
		stringBuilder.Append(now.Second.ToString("00"));
		stringBuilder.Append(now.Millisecond.ToString("000"));
		stringBuilder.Append(".png");
		return stringBuilder.ToString();
	}

	// Token: 0x060015D8 RID: 5592 RVA: 0x00093304 File Offset: 0x00091504
	public void Capture(string path = "")
	{
		if (!this.capExMode)
		{
			if (this.renderCam == null || this.renderCam.Length == 0)
			{
				if (null == Camera.main)
				{
					return;
				}
				this.renderCam = new Camera[1];
				this.renderCam[0] = Camera.main;
			}
		}
		else if (this.scriptGssAssist.Length == 0)
		{
			return;
		}
		this.savePath = path;
		if (string.Empty == this.savePath)
		{
			this.savePath = this.CreateCaptureFileName();
		}
		base.StartCoroutine(this.CaptureFunc());
	}

	// Token: 0x060015D9 RID: 5593 RVA: 0x000933A8 File Offset: 0x000915A8
	public void UnityCapture(string path = "")
	{
		this.savePath = path;
		if (string.Empty == this.savePath)
		{
			this.savePath = this.CreateCaptureFileName();
		}
		ScreenCapture.CaptureScreenshot(this.savePath, this.capRate);
	}

	// Token: 0x060015DA RID: 5594 RVA: 0x000933E4 File Offset: 0x000915E4
	private IEnumerator CaptureFunc()
	{
		yield return new WaitForEndOfFrame();
		float rate = (float)((this.capRate != 0) ? this.capRate : 1);
		Texture2D screenShot = new Texture2D((int)((float)Screen.width * rate), (int)((float)Screen.height * rate), (!this.modeARGB) ? TextureFormat.RGB24 : TextureFormat.ARGB32, false);
		int anti = (QualitySettings.antiAliasing != 0) ? QualitySettings.antiAliasing : 1;
		RenderTexture rt = RenderTexture.GetTemporary(screenShot.width, screenShot.height, 24, RenderTextureFormat.Default, RenderTextureReadWrite.Default, anti);
		Action drawCapMark = delegate()
		{
			float num = (float)Screen.width / 1280f;
			Graphics.DrawTexture(new Rect(this.texPosition.x * num, this.texPosition.y * num, (float)this.texCapMark.width * num, (float)this.texCapMark.height * num), this.texCapMark);
		};
		if (!this.capExMode)
		{
			Graphics.SetRenderTarget(rt);
			GL.Clear(true, true, Color.black);
			Graphics.SetRenderTarget(null);
			foreach (Camera camera in this.renderCam)
			{
				if (!(null == camera))
				{
					bool enabled = camera.enabled;
					RenderTexture targetTexture = camera.targetTexture;
					Rect rect = camera.rect;
					camera.enabled = true;
					camera.targetTexture = rt;
					camera.Render();
					camera.targetTexture = targetTexture;
					camera.rect = rect;
					camera.enabled = enabled;
				}
			}
			if (this.texCapMark)
			{
				Graphics.SetRenderTarget(rt);
				drawCapMark();
				Graphics.SetRenderTarget(null);
			}
		}
		else
		{
			Graphics.Blit(this.scriptGssAssist[0].rtCamera, rt);
			for (int j = 1; j < this.scriptGssAssist.Length; j++)
			{
				Graphics.DrawTexture(new Rect(0f, 0f, (float)Screen.width, (float)Screen.height), this.scriptGssAssist[j].rtCamera);
			}
			if (this.texCapMark)
			{
				drawCapMark();
			}
		}
		RenderTexture.active = rt;
		screenShot.ReadPixels(new Rect(0f, 0f, (float)screenShot.width, (float)screenShot.height), 0, 0);
		screenShot.Apply();
		RenderTexture.active = null;
		byte[] bytes = screenShot.EncodeToPNG();
		RenderTexture.ReleaseTemporary(rt);
		UnityEngine.Object.Destroy(screenShot);
		screenShot = null;
		File.WriteAllBytes(this.savePath, bytes);
		yield return null;
		yield break;
	}

	// Token: 0x040019C0 RID: 6592
	[Button("Capture", "キャプチャー", new object[]
	{
		""
	})]
	public int excuteCapture;

	// Token: 0x040019C1 RID: 6593
	[Button("UnityCapture", "Unityキャプチャー", new object[]
	{
		""
	})]
	public int excuteCaptureEx;

	// Token: 0x040019C2 RID: 6594
	public bool capExMode;

	// Token: 0x040019C3 RID: 6595
	public bool modeARGB;

	// Token: 0x040019C4 RID: 6596
	public Camera[] renderCam;

	// Token: 0x040019C5 RID: 6597
	public GameScreenShotAssist[] scriptGssAssist;

	// Token: 0x040019C6 RID: 6598
	public Texture texCapMark;

	// Token: 0x040019C7 RID: 6599
	public Vector2 texPosition = new Vector2(1152f, 688f);

	// Token: 0x040019C8 RID: 6600
	public int capRate = 1;

	// Token: 0x040019C9 RID: 6601
	private string savePath = string.Empty;
}
